package club.haomoon.boss.service.impl;

import club.haomoon.boss.service.IChannelOaReplyService;
import club.haomoon.boss.validate.base.ChannelRpValidate;
import club.haomoon.boss.validate.base.PageValidate;
import club.haomoon.boss.vo.base.ChannelRpVo;
import club.haomoon.core.common.PageResult;
import club.haomoon.core.entity.system.WechatAccountReply;
import club.haomoon.core.mapper.system.WechatAccountReplyMapper;
import club.haomoon.core.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import java.util.*;

/**
 * -------------------------
 * @Description: 公众号默认回复服务实现类
 * @Author: Wangjianwei
 * @Date: 2025/02/18 16:33
 * -------------------------
 */
@Service
public class ChannelOaReplyServiceImpl implements IChannelOaReplyService {

    @Resource
    WechatAccountReplyMapper wechatAccountReplyMapper;

    /**
     * 默认回复列表
     * @param pageValidate 分页参数
     * @return PageResult<ChannelOaReplyVo>
     */
    @Override
    public PageResult<ChannelRpVo> list(PageValidate pageValidate, Integer replyType) {
        Integer pageNo   = pageValidate.getPage_no();
        Integer pageSize = pageValidate.getPage_size();

        IPage<WechatAccountReply> iPage = wechatAccountReplyMapper.selectPage(new Page<>(pageNo, pageSize),
                new QueryWrapper<WechatAccountReply>()
                        .eq("reply_type", replyType)
                        .isNull("delete_time")
                        .orderByDesc(Arrays.asList("sort", "id")));

        List<ChannelRpVo> list = new LinkedList<>();
        for (WechatAccountReply wechatAccountReply : iPage.getRecords()) {
            ChannelRpVo vo = new ChannelRpVo();
            BeanUtils.copyProperties(wechatAccountReply, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 默认回复详情

     * @param id 主键
     * @return ChannelOaReplyDefaultVo
     */
    @Override
    public ChannelRpVo detail(Integer id) {
        WechatAccountReply wechatAccountReply = wechatAccountReplyMapper.selectOne(new QueryWrapper<WechatAccountReply>()
                .eq("id", id)
                .isNull("delete_time")
                .last("limit 1"));

        Assert.notNull(wechatAccountReply, "默认数据不存在!");

        ChannelRpVo vo = new ChannelRpVo();
        BeanUtils.copyProperties(wechatAccountReply, vo);
        return vo;
    }

    /**
     * 默认回复新增
     * @param defaultValidate 参数
     */
    @Override
    public void add(ChannelRpValidate defaultValidate) {

        if (defaultValidate.getReplyType().equals(3)) {
            if (defaultValidate.getStatus().equals(1)) {
                WechatAccountReply reply = new WechatAccountReply();
                reply.setStatus(0);
                wechatAccountReplyMapper.update(reply, new QueryWrapper<WechatAccountReply>().eq("reply_type", 3));
            }
        }

        if (defaultValidate.getReplyType().equals(2)) {
            Assert.isTrue(StringUtils.isNotEmpty(defaultValidate.getKeyword()), "关键词不能为空");
        }


        WechatAccountReply wechatAccountReply = new WechatAccountReply();
        wechatAccountReply.setReplyType(defaultValidate.getReplyType());
        wechatAccountReply.setContent(defaultValidate.getContent());
        wechatAccountReply.setName(defaultValidate.getName());
        wechatAccountReply.setContentType(defaultValidate.getContentType());
        wechatAccountReply.setStatus(defaultValidate.getStatus());
        wechatAccountReply.setKeyword(defaultValidate.getKeyword());
        wechatAccountReply.setMatchingType(defaultValidate.getMatchingType());
        wechatAccountReply.setSort(defaultValidate.getSort());
        wechatAccountReply.setUpdateTime(System.currentTimeMillis() / 1000);
        wechatAccountReply.setCreateTime(System.currentTimeMillis() / 1000);
        wechatAccountReplyMapper.insert(wechatAccountReply);
    }

    /**
     * 默认回复编辑
     * @param defaultValidate 参数
     */
    @Transactional
    @Override
    public void edit(ChannelRpValidate defaultValidate) {
        WechatAccountReply wechatAccountReply = wechatAccountReplyMapper.selectOne(new QueryWrapper<WechatAccountReply>()
                .eq("id", defaultValidate.getId())
                .isNull("delete_time")
                .last("limit 1"));

        Assert.notNull(wechatAccountReply, "默认回复数据不存在!");

        if (defaultValidate.getReplyType().equals(3)) {
            if (defaultValidate.getStatus().equals(1)) {
                WechatAccountReply reply = new WechatAccountReply();
                reply.setStatus(0);
                wechatAccountReplyMapper.update(reply, new QueryWrapper<WechatAccountReply>().eq("reply_type", wechatAccountReply.getReplyType()));
            }
        }

        if (defaultValidate.getReplyType().equals(2)) {
            Assert.isTrue(StringUtils.isNotEmpty(defaultValidate.getKeyword()), "关键词不能为空");
        }

        wechatAccountReply.setId(defaultValidate.getId());
        wechatAccountReply.setName(defaultValidate.getName());
        wechatAccountReply.setContent(defaultValidate.getContent());
        wechatAccountReply.setContentType(defaultValidate.getContentType());
        wechatAccountReply.setStatus(defaultValidate.getStatus());
        wechatAccountReply.setSort(defaultValidate.getSort());
        wechatAccountReply.setKeyword(defaultValidate.getKeyword());
        wechatAccountReply.setMatchingType(defaultValidate.getMatchingType());
//        wechatAccountReply.setReplyType(defaultValidate.getReplyType());
        wechatAccountReply.setCreateTime(System.currentTimeMillis() / 1000);
        wechatAccountReply.setUpdateTime(System.currentTimeMillis() / 1000);
        wechatAccountReplyMapper.updateById(wechatAccountReply);
    }

    /**
     * 默认回复删除
     * @param id 主键
     */
    @Override
    public void del(Integer id) {
        WechatAccountReply wechatAccountReply = wechatAccountReplyMapper.selectOne(new QueryWrapper<WechatAccountReply>()
                .eq("id", id)
                .isNull("delete_time")
                .last("limit 1"));

        Assert.notNull(wechatAccountReply, "默认回复数据不存在了!");

        wechatAccountReply.setDeleteTime(System.currentTimeMillis() / 1000);
        wechatAccountReplyMapper.updateById(wechatAccountReply);
    }

    /**
     * 默认回复状态
     * @param id 主键
     */
    @Override
    public void status(Integer id) {
        WechatAccountReply wechatAccountReply = wechatAccountReplyMapper.selectOne(new QueryWrapper<WechatAccountReply>()
                .isNull("delete_time")
                .eq("id", id)
                .last("limit 1"));

        Assert.notNull(wechatAccountReply, "默认回复数据不存在!");

        int status = wechatAccountReply.getStatus() == 1 ? 0 : 1;
        if (status == 1) {
            WechatAccountReply reply = new WechatAccountReply();
            reply.setStatus(0);
            reply.setUpdateTime(System.currentTimeMillis() / 1000);
            wechatAccountReplyMapper.update(reply, new QueryWrapper<WechatAccountReply>()
                    .eq("reply_type", wechatAccountReply.getReplyType()));
        }

        wechatAccountReply.setId(id);
        wechatAccountReply.setStatus(status);
        wechatAccountReply.setUpdateTime(System.currentTimeMillis() / 1000);
        wechatAccountReplyMapper.updateById(wechatAccountReply);
    }

}
